package cn.com.wms.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;

import cn.com.wms.Base.FilterInfo;
import cn.com.wms.Base.FilterItem;
import cn.com.wms.Base.pageInfo.PagingController;

@RestController
public class BaseController extends PagingController {
	protected Map<String, Object> queryMap = new HashMap<String, Object>();
	protected Map<String, Object> resultMap = new LinkedHashMap<String, Object>();


	/**转josn
	 * @param o
	 * @return
	 */
	public String RenderJson(Object o) {
		String s = new Gson().toJson(o);
		// System.out.println(s);
		return s;
	}
	public FilterInfo getFilterInfo(Map<String, Object> queryMap) {
		FilterInfo filter = new FilterInfo();
		for (String str : queryMap.keySet()) {
			if (queryMap.get(str) != null && !"".equals(queryMap.get(str))) {
				String[] arr = str.split("_");
				String[] arr2 = str.split("__");

				if (arr.length == 3 && queryMap.get(str) != null) {
					filter.addFilterItem(new FilterItem(arr[2], arr[0], queryMap.get(str), Integer.valueOf(arr[1])));
				} else if (arr.length > 3 && arr[3].equals("D")) {
					try {
						filter.addFilterItem(new FilterItem(arr[2], arr[0],
								new SimpleDateFormat("yyyy-MM-dd").parse(queryMap.get(str).toString()),
								Integer.valueOf(arr[1])));
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else if (arr2.length == 3 && queryMap.get(str) != null) {
					// 2016.10.13 shixuchuan
					filter.addFilterItem(new FilterItem(arr2[2], arr2[0], queryMap.get(str), Integer.valueOf(arr2[1])));
				}
			}
		}

		return filter;
	}
	
	public Map<String, Object> getQueryMap() {
		return queryMap;
	}

	public void setQueryMap(Map<String, Object> queryMap) {
		this.queryMap = queryMap;
	}
}
